﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CS2102_Music.Logic.LogicModels;

namespace CS2102_Music.Controllers
{
    public class WishListController : Controller
    {
        //
        // GET: /WishList/
        private Logic.Logic logic = Logic.Logic.GetInstance(new Logic.DbConnector());

        public ActionResult Index() // view wishlist
        {
            // user types
            UserL user = new UserL();
            if (Session["User"] != null)
            {
                user = (UserL)Session["User"];
                if (user.IsAdmin == "Y")
                    ViewBag.type = user.IsAdmin;
                else
                    ViewBag.type = "nil";

                ViewBag.logged = true;
            }
            else
            {
                ViewBag.type = "nil";
                ViewBag.logged = false;
            }

            ViewBag.typeSong = 1;
            ViewBag.typeAlbum = 2;


            // get wish list
            WishListL wishlist = new WishListL();
            wishlist = logic.GetWishListByUser(user.Email);
            ViewBag.wish = wishlist;

            return View();
        }

        public string AddToWish(int type, int productID)
        {
            string added = "";
            bool existDB = false;
            UserL user = new UserL();

            if (Session["User"] == null)
                added = "Please log in!";
            else
            {
                user = (UserL)Session["User"];

                // check if user wish before
                if (type == 1) //song
                {
                    existDB = logic.CheckUserWishedSong(user.Email, productID);
                }
                else //album
                {
                    existDB = logic.CheckUserWishedAlbum(user.Email, productID);
                }


                // add to DB wish list
                if (!existDB)
                {
                    if (type == 1)
                    {
                        logic.AddSongToWishList(user.Email, productID);
                    }
                    else
                    {
                        logic.AddAlbumToWishList(user.Email, productID);
                    }
                }


                if (existDB)
                    added = "You wished this previously";
                else
                    added = "Wished.";
            }

            return added;
        }
        public string RemoveFromWish(int type, int productID)
        {
            bool remove = false;
            UserL user = new UserL();
            string status = "";

            if (Session["User"] != null)
            {
                user = (UserL)Session["User"];

                if (type == 1)
                {
                    logic.RemoveSongFromWish(user.Email, productID);
                    remove = true;

                }
                else
                {
                    logic.RemoveAlbumFromWish(user.Email, productID);
                    remove = true;
                }
            }

            if (remove)
                status = "Un-wished successfully.";
            else
                status = "Error occurred";

            return status;
        }

    }
}
